#include <stdio.h>
#include <pthread.h>

int st[10] = {1,2,3,4,5,6,7,8,9,0};
pthread_mutex_t lock;


void*handler_print(void*arg)
{
    while(1)
    {
        pthread_mutex_lock(&lock);
        int i;
        for(i = 0;i<10;i++)
        printf("%d",st[i]);
        printf("\n");
        pthread_mutex_unlock(&lock);
        sleep(1);
    }
    pthread_exit(NULL);//exit
}

void*handler_swap(void*arg)
{
    int i,t;
    while(1)
    {
        pthread_mutex_lock(&lock);
        for(i = 0;i<5;i++)
        {
        t = st[i];
        st[i] = st[9-i];
        st[9-i] = t;
        }
        pthread_mutex_unlock(&lock);
    }
    pthread_exit(NULL);//exit
}

int main(int argc,const char*argv[])
{
    pthread_t tid1,tid2;
    if(pthread_mutex_init(&lock,NULL)<0)
    {
        perror("mutex init err");
        return -1;
    }

    if(pthread_create(&tid1,NULL,handler_swap,NULL)!=0)
    {
        perror("pthread create err");
        return -1;
    }

    if(pthread_create(&tid2,NULL,handler_print,NULL)!=0)
    {
        perror("pthread create err");
        return -1;
    }
    pthread_join(tid1,NULL);//wait
    pthread_join(tid2,NULL);//wait
    return 0;
    
}